/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.weibo.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

/**
 *
 * @author jansing lz_jansing@163.com
 */
public class BeansUtil {

    public static Object createBean(Class cla, Map<String, String[]> params) {
        try {
            Object o = cla.newInstance();
            Set<Map.Entry<String, String[]>> entrySet = params.entrySet();
            ConvertUtils.register(new DateConverter(), Date.class);
            ConvertUtils.register(new ListConverter(), List.class);
            for (Map.Entry<String, String[]> e : entrySet) {
                String[] valueStrings = e.getValue();
                if (valueStrings.length > 1) {
                    BeanUtils.copyProperty(o, e.getKey(), e.getValue());
                } else {
                    BeanUtils.copyProperty(o, e.getKey(), e.getValue()[0]);
                }
            }
            return o;
        } catch (InstantiationException ex) {
            Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
//
//    public static Object createObject(Class cla, HttpServletRequest req) {
//        try {
//            Map<String, String[]> params = req.getParameterMap();
//            Set<Map.Entry<String, String[]>> entrySet = params.entrySet();
//            Object o = cla.newInstance();
//            for (Map.Entry<String, String[]> entry : entrySet) {
//                String key = entry.getKey();
//                String value = entry.getValue()[0];
//                try {
//                    Field field = cla.getDeclaredField(key);
//                    field.setAccessible(true);
//                    Class typeClass = field.getType();
//                    if (typeClass == String.class) {
//                        field.set(o, value);
//                    } else {
//                        field.set(o, typeClass.getMethod("valueOf", String.class).invoke(typeClass, value));
//                    }
//                } catch (NoSuchFieldException ex) {
//                    System.out.println(ex.getMessage());
//                } catch (SecurityException ex) {
//                    Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (NoSuchMethodException ex) {
//                    Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (IllegalArgumentException ex) {
//                    Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (InvocationTargetException ex) {
//                    Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//            return o;
//        } catch (InstantiationException ex) {
//            Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (SecurityException ex) {
//            Logger.getLogger(BeansUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return null;
//    }
}
